package com.auxgroup.smarthome.service;

import com.auxgroup.smarthome.app.entity.AppElectricityStatisticsEntity;
import com.auxgroup.smarthome.app.repo.AppElectricityStatisticsRepo;
import com.auxgroup.smarthome.utils.NewDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by kevinchen on 2017/9/13.
 */
@Service
public class AppElectricityStatisticsService {

    @Autowired
    private AppElectricityStatisticsRepo electricityStatisticsRepo;

    private void addElectricStatisticsResult(String pk, String did, int timeType, String timeStr, Float peakVale, Float valleyValue) {
        AppElectricityStatisticsEntity electricityStatisticsEntity = electricityStatisticsRepo.findByDidAndTimeTypeAndTimeStr(did, timeType, timeStr);
        // 初始无月或者年数据的时候需要初始化
        if (electricityStatisticsEntity == null)
            electricityStatisticsEntity = new AppElectricityStatisticsEntity(pk, did, timeType, timeStr);
        electricityStatisticsEntity.addPeakTotal(peakVale);
        electricityStatisticsEntity.addValleyTotal(valleyValue);
        electricityStatisticsEntity.setPk(pk);
        electricityStatisticsRepo.save(electricityStatisticsEntity);
    }

    /**
     * 统计并保存波峰波谷波平的日用电总量
     * 时间格式：格式"yyyyMMdd" :20170914
     *
     * @param pk
     * @param did
     * @param peakVale
     * @param valleyValue
     * @param timeBucket
     */
    public void saveDayElectricStaticsResult(String pk, String did, Float peakVale, Float valleyValue, String timeBucket) {
        AppElectricityStatisticsEntity electricityStatisticsEntity = electricityStatisticsRepo.findByDidAndTimeTypeAndTimeStr(did, 0, NewDateUtils.getYesterdayStr());
        if (electricityStatisticsEntity!=null)return;
        AppElectricityStatisticsEntity appElectricityStatisticsEntity = new AppElectricityStatisticsEntity(pk, did, 0,
                    NewDateUtils.getYesterdayStr(), peakVale, valleyValue, timeBucket);
        electricityStatisticsRepo.save(appElectricityStatisticsEntity);
    }

    /**
     * 累加保存波峰波谷波平的月用电总量
     * monthStr-格式"yyyyMM" :201709
     *
     * @param pk
     * @param did
     * @param peakVale
     * @param valleyValue
     */
    public void addMothElectricStatisticsResult(String pk, String did, Float peakVale, Float valleyValue) {
        addElectricStatisticsResult(pk, did, 1, NewDateUtils.getYesterdayMonthStr(), peakVale, valleyValue);
    }

    /**
     * 累加保存波峰波谷波平的年用电总量
     * yearStr - 格式 "yyyy" : 2017
     *
     * @param pk
     * @param did
     * @param peakVale
     * @param valleyValue
     */
    public void addYearElectricStatisticsResult(String pk, String did, Float peakVale, Float valleyValue) {
        addElectricStatisticsResult(pk, did, 2, NewDateUtils.getYesterdayYearStr(), peakVale, valleyValue);
    }

    /**
     * 删除4个月前的数据
     *
     * @param did
     */
    public void deleteByDidAndTimeStr(String did) {
        electricityStatisticsRepo.deleteByDidAndTimeStr(did, NewDateUtils.getExpireDayStr(4));
    }


}
